var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()

Page({
  data: {
    logoutImg: ['../pageImages/new/head.png'],
    indicatorDots: false,
    vertical: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userId: null,
    sex: null,
    memberName: null,
    partyOrgName: null
  },
  onLoad: function () {
    this.setData({
      icon20: base64.icon20,
      icon60: base64.icon60
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow: function () {
    wx.setNavigationBarColor({
      backgroundColor: '#C40002',
      frontColor: '#ffffff'
    })
    if (app.globalData.userId != null) {
      this.setData({
        userId: app.globalData.userId
      });
    }
    if (app.globalData.memberName) {
      this.setData({
        memberName: app.globalData.memberName
      });
    } 
    if (app.globalData.partyOrgName) {
      this.setData({
        partyOrgName: app.globalData.partyOrgName
      });
    }
    if (app.globalData.sex != -1) {
      this.setData({
        sex: app.globalData.sex
      });
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      app.globalData.userInfo = e.detail.userInfo;
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      });
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    } else {
      //用户按了拒绝按钮
    }
    
  },
  bindUser: function(){
    wx.navigateTo({
      url: '../binduser/binduser'
    })
  },
  toPersonInfo: function () {
    if (this.data.userId == null) {
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    }
    wx.navigateTo({
      url: '../personinfo/personinfo'
    })
  },
  toLike: function () {
    if (this.data.userId == null) {
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    }
    wx.navigateTo({
      url: '../like/like'
    })
  },
  toSkill: function () {
    if (this.data.userId == null) {
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    }
    wx.navigateTo({
      url: '../skill/skill'
    })
  },
  toGreatful: function () {
    if (this.data.userId == null) {
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    }
    wx.navigateTo({
      url: '../greatful/greatful'
    })
  },
  toMyorg: function () {
    if (this.data.userId == null) {
      wx.navigateTo({
        url: '../binduser/binduser'
      })
    }
    wx.navigateTo({
      url: '../myorg/myorg'
    })
  },
  noContent1: function () {
    wx.navigateTo({
      url: '../404/noContent?pageType=我的特长'
    })
  },
  toCollect: function () {
    wx.navigateTo({
      url: '../collect/collect'
    })
  },
  noContent3: function () {
    wx.navigateTo({
      url: '../404/noContent?pageType=通讯录'
    })
  },
  noContent4: function () {
    wx.navigateTo({
      url: '../404/noContent?pageType=党员风采'
    })
  },
  toPhoneBook: function () {
    wx.navigateTo({
      url: '../phoneBook/phoneBook'
    })
  }
})
